package com.wiscamp.ninechapters.tags.adapter.resources;


import com.wiscamp.ninechapters.tags.application.dto.command.CreateTagCommand;
import com.wiscamp.ninechapters.tags.application.dto.command.UpdateTagCommand;
import com.wiscamp.ninechapters.tags.application.dto.vo.TagGroupVO;
import com.wiscamp.ninechapters.tags.application.dto.vo.TagVO;
import com.wiscamp.ninechapters.tags.application.services.TagAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 标签服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("api/Tags")

@Tag(name = "标签知识点-标签信息")
public class TagController extends BaseController {

    private final TagAppService tagAppService;

    public TagController(TagAppService tagAppService) {
        this.tagAppService = tagAppService;
    }

    /**
     * 通过标签编号获取标签
     *
     * @param request
     * @param response
     * @param tagId
     * @return
     */
    @GetMapping("Tags/{tagId}")
    @Operation(summary = "通过标签编号获取标签")
    @HandleResourceException
    public RestResponse<TagVO> getTag(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                      @PathVariable("tagId") long tagId) {
        return ResourceUtils.getById(request, response, tagId, tagAppService::getTag);
    }

    /**
     * 通过标签组编号和代码获取标签
     *
     * @param appId
     * @param tagCode
     * @return
     */

    @GetMapping("Applications/{appId}/Tags/Code/{tagCode}")
    @Operation(summary = "通过标签组编号和代码获取标签")
    @HandleResourceException
    public RestResponse<TagVO> getTagByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                            @PathVariable("appId") long appId, @PathVariable("tagCode") String tagCode) {
        return ResourceUtils.getByTextAndId(request, response, tagCode, appId, tagAppService::getTagByCode);
    }


    /**
     * 通过标签组编号和名称获取标签列表
     *
     * @param appId
     * @param groupName
     * @param tagName
     * @return
     */
    @GetMapping("Applications/{appId}/Tags/Name/{groupName}/{tagName}")
    @Operation(summary = "通过标签组编号和名称获取标签列表")
    @HandleResourceException
    public RestResponse<TagVO> getTagsByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                             @PathVariable("appId") long appId, @PathVariable("groupName") String groupName, @PathVariable("tagName") String tagName) {
        var tagVO = tagAppService.getTagByName(groupName, tagName, appId);
        return ResourceUtils.getOkResponse(request, response, tagVO);
    }

    /**
     * 通过标签组名称获取标签列表
     *
     * @param appId
     * @param groupName
     * @return
     */
    @GetMapping("Applications/{appId}/Tags/GroupName/{groupName}")
    @Operation(summary = "通过标签组名称获取标签列表")
    @HandleResourceException
    public RestResponse<Collection<TagVO>> getTagsByGroupName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                              @PathVariable("appId") long appId,
                                                              @PathVariable("groupName") String groupName) {
        return ResourceUtils.getListByTextAndId(request, response, groupName, appId, tagAppService::getTagsByGroupName);
    }


    /**
     * 通过标签编号删除标签
     *
     * @param id
     * @return
     */
    @PutMapping("Tags/Actions/Delete")
    @Operation(summary = "删除标签")
    @HandleResourceException
    public RestResponse<Boolean> deleteTag(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                           @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, tagAppService::deleteTag);
    }

    @PutMapping("Tags/Actions/Enable")
    @Operation(summary = "启用标签")
    @HandleResourceException
    public RestResponse<Boolean> enableTag(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                           @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, tagAppService::enableTag);
    }

    @PutMapping("Tags/Actions/Disable")
    @Operation(summary = "启用标签")
    @HandleResourceException
    public RestResponse<Boolean> disableTag(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                            @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, tagAppService::disableTag);
    }

    /**
     * 创建标签
     *
     * @param command
     * @return
     */
    @PostMapping("Tags")
    @Operation(summary = "创建标签")
    @HandleResourceException
    public RestResponse<TagVO> createTag(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                         @RequestBody CreateTagCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, tagAppService::createTag);
    }

    /**
     * 更新标签
     *
     * @param command
     * @return
     */
    @PutMapping("Tags")
    @Operation(summary = "更新标签")
    @HandleResourceException
    public RestResponse<TagVO> updateTag(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                         @RequestBody UpdateTagCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, tagAppService::updateTag);
    }


    @GetMapping("/TagGroups/Available")
    @Operation(summary = "获取可用标签组列表，兼容旧接口")
    @HandleResourceException
    public RestResponse<Collection<TagGroupVO>> getTagsByGroupName(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, tagAppService::getAvailableTagGroups);
    }
}
